using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO.Filtron
{
    public class FiltronMakeDAO : Abstracts.AbstractGenericDAO<FiltronMake, System.Nullable<int>>,
        Interfaces.Filtron.IFiltronMakeDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "make_id";
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie pojazdu.
        /// </summary>
        /// <param name="vehicleId">pojazd</param>
        /// <returns>rekordy o zadanym pojezdzie lub null, gdy nie ma</returns>
        public IQueryable<FiltronMake> FindByVehicle(int vehicleId)
        {
            if (vehicleId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<FiltronMake> makes = this._cmsEntitiesDataContext.FiltronMakes.Where(
                    make => make.vehicle_id.Equals(vehicleId)
                        ).OrderBy(make => make.make_name);
                if (!(makes.Any()))
                {
                    return null;
                }   //if
                return makes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie filtru.
        /// </summary>
        /// <param name="filterId">model samochodu</param>
        /// <returns>rekordy o zadanym filtrze lub null, gdy nie ma</returns>
        public IQueryable<FiltronMake> FindByFilter(int filterId)
        {
            if (filterId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<FiltronMake> makes = this._cmsEntitiesDataContext.FiltronMakes.Where(
                    make => this._cmsEntitiesDataContext.FiltronModels.Where(mode =>
                        this._cmsEntitiesDataContext.FiltronEngineModels.Where(enmo =>
                        this._cmsEntitiesDataContext.FiltronFilterEngines.Where(fien =>
                        fien.filter_id.Equals(filterId)).Select(fe => fe.engine_id).Contains(enmo.engine_id)
                        ).Select(em => em.model_id).Contains(mode.model_id)
                        ).Select(mo => mo.make_id).Contains(make.make_id)
                        );                       
                        
                if (!(makes.Any()))
                {
                    return null;
                }   //if
                return makes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<FiltronMake> FindByModel(int modelId)
        {
            if (modelId.Equals(0))
            {
                return null;
            }   //if
            try
            {

                CMSEntitiesDataContext c = this._cmsEntitiesDataContext;
                var make = 
                    from m in c.FiltronMakes
                    join model in c.FiltronModels 
                    on m.make_id equals model.make_id
                    where model.model_id.Equals(modelId)
                    select m;

                        
                if (!(make.Any()))
                {
                    return null;
                }   //if
                return make;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }
        
    }
}
